package com.tan.lbs.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;

import com.tan.lbs.dao.GameConditionDAO;
import com.tan.lbs.entity.GameCondition;
import com.tan.lbs.entity.GameTrigger;
import com.tan.lbs.entity.Item;
import com.tan.lbs.util.HibernateUtil;

public class GameConditionDAOImpl implements GameConditionDAO {

	@Override
	public String create(GameCondition gameCondition) {
		SessionFactory sf = HibernateUtil.getSessionFactory();
		Session s = sf.getCurrentSession();
		s.beginTransaction();
		s.save(gameCondition);
		s.getTransaction().commit();
		return gameCondition.getConditionID();
	}

	@Override
	public GameCondition readByID(String conditionID) {
		SessionFactory sf = HibernateUtil.getSessionFactory();
		Session s = sf.getCurrentSession();
		s.beginTransaction();
		GameCondition gameCondition = (GameCondition)s.get(GameCondition.class, conditionID);
		s.getTransaction().commit();
		return gameCondition;
	}

	@Override
	public void update(GameCondition gameCondition) {
		SessionFactory sf = HibernateUtil.getSessionFactory();
		Session s = sf.getCurrentSession();
		s.beginTransaction();
		s.update(gameCondition);
		s.getTransaction().commit();
	}

	@Override
	public void delete(String conditionID) {
		SessionFactory sf = HibernateUtil.getSessionFactory();
		Session s = sf.getCurrentSession();
		s.beginTransaction();
		GameCondition gameCondition = new GameCondition();
		gameCondition.setConditionID(conditionID);
		s.delete(gameCondition);
		s.getTransaction().commit();

	}

	@Override
	public ArrayList<GameCondition> searchByGameTrigger(String triggerID) {
		SessionFactory sf = HibernateUtil.getSessionFactory();
		Session s = sf.getCurrentSession();
		s.beginTransaction();
		List<GameCondition> gs = s.createCriteria(GameCondition.class).add(Restrictions.
				eq("triggerID", triggerID)).list();
		s.getTransaction().commit();
		return new ArrayList<GameCondition>(gs);
	}

	@Override
	public void deleteByGameObject(String gameObjectID) {
		ArrayList<GameCondition> conditions = searchByGameObject(gameObjectID);
		SessionFactory sf = HibernateUtil.getSessionFactory();
		Session s = sf.getCurrentSession();
		s.beginTransaction();
		for(GameCondition gameCondition : conditions) {
			GameCondition deleteCondition = new GameCondition();
			deleteCondition.setConditionID(gameCondition.getConditionID());
			s.delete(gameCondition);
		}
		s.getTransaction().commit();
		
	}

	@Override
	public ArrayList<GameCondition> searchByGameObject(String gameObjectID) {
		SessionFactory sf = HibernateUtil.getSessionFactory();
		Session s = sf.getCurrentSession();
		s.beginTransaction();
		List<GameCondition> gs = s.createCriteria(GameCondition.class).add(Restrictions.
				eq("paramX", gameObjectID)).list();
		s.getTransaction().commit();
		return new ArrayList<GameCondition>(gs);
	}

}
